﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using BUS;

namespace CustomerManager
{
    public partial class ucCompany : UserControl
    {
        Int32 manhanvien;
        public void ChiTietDoanhNghiep(Company cm)
        {
            txtChuTichHDQT.Text = cm.ChuTichHDQT;
            txtCoQuanCap.Text = cm.CoQuanCap;
            txtCoQuanQuanLy.Text = cm.CoQuanQLyTrucTiep;
            txtDiaChi.Text = cm.DiaChi;
            txtGiamDoc.Text = cm.TongGiamDoc;
            txtGiayChungNhan.Text = cm.GiayPhepKinhDoanh;
            txtLoaiHinhDoanhNghiep.Text = cm.LoaiHinh;
            txtMaDoanhNghiep.Text = cm.Ma.ToString();
            txtMaSoThue.Text = cm.MaSoThue;
            txtFax.Text = cm.Fax;
            txtQuyetDinhSo.Text = cm.QuyetDinhThanhLap;
            txtSoDienThoai.Text = cm.DienThoai;
            txtSoTienCamKet.Text = cm.HanMuc.ToString();
            txtTenDoanhNghiep.Text = cm.Ten;
            txtTongSoLaoDong.Text = cm.TongLaoDong.ToString();
            txtVonDangKy.Text = cm.VonKinhDoanh.ToString();
            txtVonDieuLe.Text = cm.VonDieuLe.ToString();
            cmbNganhKinhDoanh.Text = cm.NganhKinhDoanh;
            cmbNganhKinhTe.Text = cm.NganhKinhTe;
            manhanvien = cm.MaNhanVien;
        }


        private void ValidateControls(bool enable)
        {
            txtChuTichHDQT.Enabled = enable;
            txtCoQuanCap.Enabled = enable;
            txtCoQuanQuanLy.Enabled = enable;
            txtDiaChi.Enabled = enable;
            txtGiamDoc.Enabled = enable;
            txtGiayChungNhan.Enabled = enable;
            txtLoaiHinhDoanhNghiep.Enabled = enable;
            txtMaDoanhNghiep.Enabled = enable;
            txtMaSoThue.Enabled = enable;
            txtFax.Enabled = enable;
            txtQuyetDinhSo.Enabled = enable;
            txtSoDienThoai.Enabled = enable;
            txtSoTienCamKet.Enabled = enable;
            txtTenDoanhNghiep.Enabled = enable;
            txtTongSoLaoDong.Enabled = enable;
            txtVonDangKy.Enabled = enable;
            txtVonDieuLe.Enabled = enable;
            dtpChuTichHDQT.Enabled = enable;
            dtpGiamDoc.Enabled = enable;
            dtpGiayChungNhan.Enabled = enable;
            dtpQuyetDinhSo.Enabled = enable;
            cmbNganhKinhDoanh.Enabled = enable;
            cmbNganhKinhTe.Enabled = enable;
        }

        public ucCompany()
        {
            InitializeComponent();
        }
        ucCustomer frm;
        public ucCompany(FrmMain.ExeAction act, ucCustomer f)
        {
            InitializeComponent();
            frm = f;
            if (act == FrmMain.ExeAction.Add)
            {
                btnSua.Visible = false;
                btnXoa.Visible = false;
                ValidateControls(true);
            }
            else if (act == FrmMain.ExeAction.View)
            {
                btnLuu.Visible = false;
                ValidateControls(false);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "Sửa thông tin")
            {
                ValidateControls(true);
                txtMaDoanhNghiep.Enabled = false;
                btnSua.Text = "Lưu thay đổi";
            }
            else if (btnSua.Text == "Lưu thay đổi")
            {
                try
                {
                    Company cm = new Company();
                    cm.ChuTichHDQT = txtChuTichHDQT.Text;
                    cm.CoQuanCap = txtCoQuanCap.Text;
                    cm.CoQuanQLyTrucTiep = txtCoQuanQuanLy.Text;
                    cm.DiaChi = txtDiaChi.Text;
                    cm.DienThoai = txtSoDienThoai.Text;
                    cm.Fax = txtFax.Text;
                    cm.GiayPhepKinhDoanh = txtGiayChungNhan.Text;
                    cm.HanMuc = Int32.Parse(txtSoTienCamKet.Text);
                    cm.LoaiHinh = txtLoaiHinhDoanhNghiep.Text;
                    cm.Ma = txtMaDoanhNghiep.Text;
                    cm.MaNhanVien = manhanvien;
                    cm.MaSoThue = txtMaSoThue.Text;
                    cm.NamSinhChuTichHDQT = dtpChuTichHDQT.Value;
                    cm.NamSinhTongGiamDoc = dtpGiamDoc.Value;
                    cm.NganhKinhDoanh = cmbNganhKinhDoanh.Text;
                    cm.NganhKinhTe = cmbNganhKinhTe.Text;
                    cm.NgayCapGiayPhep = dtpGiayChungNhan.Value;
                    cm.NgayQuyetDinh = dtpQuyetDinhSo.Value;
                    cm.QuyetDinhThanhLap = txtQuyetDinhSo.Text;
                    cm.Ten = txtTenDoanhNghiep.Text;
                    cm.TongGiamDoc = txtGiamDoc.Text;
                    cm.TongLaoDong = Int32.Parse(txtTongSoLaoDong.Text);
                    cm.VonDieuLe = Int32.Parse(txtVonDieuLe.Text);
                    cm.VonKinhDoanh = Int32.Parse(txtVonDangKy.Text);

                    CompanyBUS.CapNhatKHDoanhNghiep(cm);

                    MessageBox.Show("Thay đổi thành công!");
                    ValidateControls(false);
                    frm.LoadCustomer(manhanvien);
                    btnSua.Text = "Sửa thông tin";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnTroLai_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                String makhdoanhnghiep = txtMaDoanhNghiep.Text;
                if (MessageBox.Show("Bạn có chắc xóa khách hàng này không?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    CompanyBUS.XoaKHDoanhNghiep(makhdoanhnghiep);
                    MessageBox.Show("Xóa thành công");
                    frm.LoadCustomer(manhanvien);
                    this.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                Company cm = new Company();
                cm.ChuTichHDQT = txtChuTichHDQT.Text;
                cm.CoQuanCap = txtCoQuanCap.Text;
                cm.CoQuanQLyTrucTiep = txtCoQuanQuanLy.Text;
                cm.DiaChi = txtDiaChi.Text;
                cm.DienThoai = txtSoDienThoai.Text;
                cm.Fax = txtFax.Text;
                cm.GiayPhepKinhDoanh = txtGiayChungNhan.Text;
                cm.HanMuc = Int32.Parse(txtSoTienCamKet.Text);
                cm.LoaiHinh = txtLoaiHinhDoanhNghiep.Text;
                cm.Ma = txtMaDoanhNghiep.Text;
                cm.MaNhanVien = manhanvien;
                cm.MaSoThue = txtMaSoThue.Text;
                cm.NamSinhChuTichHDQT = dtpChuTichHDQT.Value;
                cm.NamSinhTongGiamDoc = dtpGiamDoc.Value;
                cm.NganhKinhDoanh = cmbNganhKinhDoanh.Text;
                cm.NganhKinhTe = cmbNganhKinhTe.Text;
                cm.NgayCapGiayPhep = dtpGiayChungNhan.Value;
                cm.NgayQuyetDinh = dtpQuyetDinhSo.Value;
                cm.QuyetDinhThanhLap = txtQuyetDinhSo.Text;
                cm.Ten = txtTenDoanhNghiep.Text;
                cm.TongGiamDoc = txtGiamDoc.Text;
                cm.TongLaoDong = Int32.Parse(txtTongSoLaoDong.Text);
                cm.VonDieuLe = Int32.Parse(txtVonDieuLe.Text);
                cm.VonKinhDoanh = Int32.Parse(txtVonDangKy.Text);

                CompanyBUS.ThemKHDoanhNghiep(cm);

                MessageBox.Show("Thêm khách hàng thành công!");
                ValidateControls(false);
                frm.LoadCustomer(manhanvien);
                btnLuu.Hide();
                btnSua.Text = "Sửa thông tin";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
